<?php
namespace app\back\controller;

use app\back\model\CommentModel;
use think\Request;


///留言管理
class Leave extends AdminCommon
{

    ///留言列表
    public function index(CommentModel $commentModel){
        ///渲染页面
        $this->assign([
            'indexDataList'=>$commentModel->getCommentData(),
        ]);
        return view();
    }

    ///获取当前回复操作的留言
    public function reply(Request $request,CommentModel $commentModel){
        ///分类筛选
        $comm_id = $request->param('id',0,'intval');
        ///渲染页面
        $this->assign([
            'one_data'=>$commentModel->getOneData($comm_id),
        ]);
        return view();
    }
    ///回复留言
    public function replyPost(Request $request,CommentModel $commentModel){
        $repay_id = $request->param('repay_id',0,'intval');
        $repay_content = $request->param('repay_content','');

        ///效验
        if(empty($repay_content)){return think_json_encode(['status'=>2,'msg'=>'回复也不能是空的吧！']);}
        if($repay_id == 0){return think_json_encode(['status'=>2,'msg'=>'出现点状况，请检查！']);}
        ///操作数据库
        try{
            ///修改回复记录状态
            $commentModel->updateReplyStatus($repay_id);

            ///入库回复数据
            $commentModel->insertManagerReplyData($repay_id,$repay_content);
            return think_json_encode(['status'=>0,'msg'=>'操作成功！']);
        }catch(\Exception $e){
            trace($e->getMessage(),'error');
            return think_json_encode(['status'=>2,'msg'=>'操作出现异常，请检查啊！']);
        }

    }



    ///留言状态
    public function review(Request $request,CommentModel $commentModel){
        ///分类筛选
        $comm_id = $request->param('id',0,'intval');
        ///渲染页面
        $this->assign([
            'one_data'=>$commentModel->getOneData($comm_id),
        ]);
        return view();
    }
    ///留言状态操作
    public function reviewPost(Request $request,CommentModel $commentModel){
        $repay_id = $request->param('repay_id',0,'intval');
        $old_show_status = $request->param('old_show_status',0,'intval');
        $com_show_status = $request->param('com_show_status',0,'intval');

        ///效验
        if($repay_id == 0){return think_json_encode(['status'=>2,'msg'=>'出现点状况，请检查！']);}
        ///操作数据库
        try{
            ///修改回复记录状态
            $commentModel->updateStatusByIDAndStatus($repay_id,$old_show_status,$com_show_status);
            return think_json_encode(['status'=>0,'msg'=>'操作成功！']);
        }catch(\Exception $e){
            trace($e->getMessage(),'error');
            trace(M()->getLastSql(),'error');
            return think_json_encode(['status'=>2,'msg'=>'操作出现异常，请检查啊！']);
        }

    }


}